package com.android.takegism.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.takegism.R;
import com.android.takegism.service.PuzzleService;
import com.android.takegism.utils.Clue;
import com.android.takegism.utils.EasyPuzzle;
import com.android.takegism.utils.MiddlePuzzle;
import com.android.takegism.utils.MyContext;
import com.android.takegism.utils.PuzzleAbstract;
import com.android.takegism.utils.UIHandler;

public class SolvePuzzleActivity extends AppCompatActivity {

    private static final int UPDATE_PUZZLE = 1234;
    private static PuzzleAbstract puzzle;
    static TextView question_text;

    private PuzzleService.PuzzleBinder puzzleBinder;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            puzzleBinder = (PuzzleService.PuzzleBinder) service;
            Log.d("ZZJ", "连接成功" + puzzleBinder.toString());
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }

    public static UIHandler uiHandler = new UIHandler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == UPDATE_PUZZLE) {
                Bundle extras = (Bundle) msg.obj;
                int hardLevel = (int) MyContext.getInstance().getStatics("hardLevel");
                Clue clue = (Clue) extras.get("clue");

                switch (hardLevel) {
                    case 1:
                        puzzle = new EasyPuzzle(clue);
                        puzzle.setClue(clue);
                        break;
                    case 2:
                        puzzle = new MiddlePuzzle(clue);
                        puzzle.setClue(clue);
                        break;
                    case 3:
                        puzzle = new MiddlePuzzle(clue);
                        //TODO
                        puzzle.setClue(clue);
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + hardLevel);
                }

                question_text.setText(puzzle.getQuestion());
                super.handleMessage(msg);
            }

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sovle_puzzle);

//        bindService(new Intent(this, PuzzleService.class), serviceConnection, BIND_AUTO_CREATE);
//        Bundle extras = puzzleBinder.getBundle();

/*        extras = new Bundle();
        Clue test_clue = new Clue(R.mipmap.ic_launcher, "FUCK");
        extras.putSerializable("clue",test_clue);
        int hardLevel = extras.getInt("hardLevel", 1);
        Clue clue = (Clue) extras.get("clue");

        switch (hardLevel) {
            case 1:
                puzzle = new EasyPuzzle(clue);
                puzzle.setClue(clue);
                break;
            case 2:
                puzzle = new MiddlePuzzle(clue);
                puzzle.setClue(clue);
                break;
        }*/
        TextView question_text = findViewById(R.id.question_text);
        question_text.setText(puzzle.getQuestion());
        Button button = findViewById(R.id.commit_puzzle_zzj);
        EditText answer_text = findViewById(R.id.answer_text);
        TextView clue_show_text = findViewById(R.id.clue_show_text);
        ImageView clue_show_image = findViewById(R.id.clue_show_image);
        question_text = findViewById(R.id.question_text);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ans = answer_text.getText().toString();
                String check = null;
                if (puzzle == null) {
                    return;
                }
                try {
                    check = (String) puzzle.check(ans);
                    if (check == null || check.equals("")) {
                        check = "答案错误";
                    } else {
                        int imageId = puzzle.getClue().getImageId();
                        if (imageId != 0) {
                            clue_show_image.setImageResource(imageId);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                clue_show_text.setText(check);
            }
        });

        Button test_my_clues = findViewById(R.id.test_my_clues);
        test_my_clues.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SolvePuzzleActivity.this, MyCluesActivity.class));
            }
        });
    }
}